package Lintcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Question: https://www.lintcode.com/zh-cn/old/problem/flatten-list/
 * Solution:
 * Time: 2018/5/17
 * Coder: Ksxy 
**/
public class FattenList {
    static Scanner scanner = null;
    static {
        scanner = new Scanner(System.in);
    }

    public static void main(String[] args) {

    }

    public List<Integer> flatten(List<NestedInteger> nestedList) {
        List<Integer> list = new ArrayList<>();
        flattenRecursive(nestedList, list);
        return list;
    }

    private void flattenRecursive(List<NestedInteger> nestedList, List<Integer> list) {
        for (NestedInteger item : nestedList) {
            if(item.isInteger()){
                list.add(item.getInteger());
            }else{
                flattenRecursive(item.getList(), list);
            }
        }
    }

    public static interface NestedInteger {

        // @return true if this NestedInteger holds a single integer,
        // rather than a nested list.
        public boolean isInteger();

        // @return the single integer that this NestedInteger holds,
        // if it holds a single integer
        // Return null if this NestedInteger holds a nested list
        public Integer getInteger();

        // @return the nested list that this NestedInteger holds,
        // if it holds a nested list
        // Return null if this NestedInteger holds a single integer
        public List<NestedInteger> getList();
    }
}
